package com.wr.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王瑞
 * @description: 自定义拦截器
 * @date 2020/4/16  15:16
 */
@Activate(group = {CommonConstants.PROVIDER})
public class TPMonitorFilter implements Filter {

    private final static double TP90 = 0.9;
    private final static double TP99 = 0.99;


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        long startTime = System.currentTimeMillis();
        try {
            return invoker.invoke(invocation);
        } finally {
            long endTime = System.currentTimeMillis();
            long res = endTime - startTime;
            double sumTime = res * 1.0 / 100 * 1.0;

            String methodName = invocation.getMethodName();
            if (sumTime >= TP90 && sumTime < TP99) {
                //放到队列TP90
                TPMonnitotQueue.getTP90queueMap(methodName).add(sumTime);

            }
            if (sumTime >= TP99) {
                //放到队列TP99
                TPMonnitotQueue.getTP99queueMap(methodName).add(sumTime);
            }
        }
    }




}
